<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>产品品牌</title>
<script src="/res/third/bootstrap3/bootstrap.autocomplete.js" type="text/javascript"></script> 
</head>
<body>
<div class="panel panel-info">
        <!-- Default panel contents -->
        <div class="panel-heading">
				  <button type="button" class="btn btn-info" id="addButton" onclick="addBrand();">
				  	<span class="glyphicon glyphicon-plus"></span>&nbsp;新增
				  </button>
				  <button type="button" class="btn btn-danger" id="addButton" onclick="deleteBrandMore();">
				  	<span class="glyphicon glyphicon-minus"></span>&nbsp;批量删除
				  </button>
				 <!--  <button type="button" class="btn btn-default" id="editButton">编辑</button>
				  <button type="button" class="btn btn-default" id="delButton">删除</button> -->
        </div>
        <!-- Table -->
        <table class="table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th width="50" ><input type="checkbox" class="check_all_brand" /></th>
              <th width="80" >序号</th>
              <th >品牌名称</th>
              <th style="text-align: center" width="100">排序号</th>
              <th style="text-align: center" width="200">创建时间</th>
              <th style="width:150px;"  >操作</th>
            </tr>
          </thead>
          <tbody class="brandtbody">
          	[#list p.list as brand]
          		<tr>
          			<td align="center"><input type="checkbox" _brd="${brand.id!}"/></td>
          			<td align="center">${brand_index+1}</td>
	          		<td align="center">${(brand.brand.name)!}</td>
	          		<td align="center">${brand.seq!0}</td>
	          		<td align="center">[#if brand.createtime??]${brand.createtime!?string('yyyy-MM-dd')}[/#if]</td>
	          		<td align="center">
		          		<button type="button" class="btn btn-info btn-xs edits" onclick="editBrand(${brand.id} , ${brand.seq!0} , '${brand.brand.name!}' , ${brand.brand.id})">编辑</button>
		          		<button type="button" class="btn btn-danger btn-xs delbtn" onclick="delBrand(${brand.id})">删除</button>
	          		</td>
	          	<tr>
          	[/#list]
          </tbody>
        </table>
       <div class="panel-footer">[@fenye p = p form = 'brandinfoForm' mulity = 'brand'/]
       </div>
      </div>

<!-- 新建 -->
<div id="createBrand"  style="display: none;"   >
	<form  class="form-horizontal" name="brankForm" role="form">
	<input type="hidden"  name="id"  id="bid"/>
	<input type="hidden"  name="categoryTId"  id="bpid" value="${(p.params['so_pid'])!}"/>
	  <div class="form-group">
	    <label for="title" >品牌</label>
	      <input class="form-control" id="brandid"  name="brandId" required  />
	  </div>
	  <div class="form-group">
	    <label for="title" >排序号</label>
	      <input class="form-control" id="seq"  name="seq" required />
	  </div>
	  <div class="form-group">
	    <div style="text-align: center;" >
	      <button  onclick="subBrand(this);"  type="button" class="btn btn-info">提交</button>
	    </div>
	  </div>
	</form>
</div>
</body>
</html>
<script type="text/javascript">
		$(".check_all_brand").click(function(){
			if(this.checked){
				$(".brandtbody input[type='checkbox']").prop("checked" , true);
			}else{
				$(".brandtbody input[type='checkbox']").prop("checked" , false);
			}
		});
		
		
		function deleteBrandMore(){
			if($(".brandtbody input[type='checkbox']:checked").length > 0){
				var ids = [];
				$(".brandtbody input[type='checkbox']:checked").each(function(){
					ids.push($(this).attr("_brd"));
				});
				artDialog.confirm('是否确认批量删除' , function(){
					$.ajax({
						url : '/categoryBrand/o_delete_more.do',
						type : 'post',
						dataType : 'json',
						data : {bids : ids.join(",")},
						success : function(data){
							if(data.success){
								refeshBrand();
								}else{
								   artDialog.alert(data.msg , function(){refeshBrand();});
								}
						}
					});
				});
			}else{
				artDialog.alert("请选择" , function(){});
			}
		}

		$(function(){
			$('#brandid').autocomplete({
				items : 5,
		        source:function(query,process){
		            var matchCount = this.options.items;//返回结果集最大数量
		            $.post("/categoryBrand/get_brand.do",{"queryName":query,"pageno":matchCount},function(respData){
		            	console.log(respData.length);
		                return process($.parseJSON(respData));
		            });
		        },
		        formatItem:function(item){
		            return item["name"];
		        },
		        setValue:function(item){
		            return {'data-value':item["name"],'real-value':item["id"]};
		        }
		    });
		});
		function refeshBrand(){
			$.messager.progress({
				text : "loadding",
				interval : 100
			});
			$("#brand").load('/categoryBrand/v_list.do' , $("form[id='brandinfoForm']").serialize() , function(){
				$.messager.progress('close');
			});
		}
		
		function brandgo(pageNo){
			$("#brandinfoForm input[name='pageNo']").val(pageNo);
			refeshBrand();
		}
		function brandrefresh(){
			brandgo(${p.pageNo});
		}
		
	var dialog;
		
	function addBrand(){
		document.brankForm.reset();
		dialog = art.dialog({
			        title: '新增品牌',
			        width:500,
			        lock : true,
			        opacity : 0.3,
			        content: document.getElementById('createBrand')
			    });
	}

	function editBrand(id , seq , brandname , brandid){
		document.brankForm.reset();
		$("#seq").val(seq);
		$("#bid").val(id);
		$("#brandid").val(brandname);
		dialog = art.dialog({
	        title: '编辑品牌',
	        width:500,
	        lock : true,
	        opacity : 0.3,
	        content: document.getElementById('createBrand'),
	    });
	}

	function subBrand(obj){
		$(obj).attr("disabled" , true);
		var brandid = $("#brandid").attr("real-value");
		if(!brandid || brandid == ""){
			artDialog.alert("请输入搜索品牌名称并在列表中选择");
			$(obj).attr("disabled" , false);
			return false;
		}
		var seq = $("#seq").val();
		if(seq == "" || !/^\d+$/.test(seq) || !(seq > 0)){
			artDialog.alert("请输入正确的排序号");
			$(obj).attr("disabled" , false);
			return false;
		}
		
		$.ajax({
			url : '/categoryBrand/o_add.do',
			type : 'post',
			dataType : 'json',
			data : {
				id : $("#bid").val(),
				brandId : brandid,
				categoryTId : $("#bpid").val(),
				seq : $("#seq").val().trim()
			},
			beforeSend:function(){
	            //art.dialog.tips("编辑中...");
	        },
			success : function(data){
					dialog.close();
					if(data.success){
		               refeshBrand();
					}else{
						artDialog.alert(data.msg , function(){refeshBrand();});
					}
			}
		});
	}
	
	function delBrand(id){
		artDialog.confirm('是否确认删除' , function(){
				$.ajax({
					url : '/categoryBrand/o_del.do',
					type : 'post',
					dataType : 'json',
					data : {
						id : id
					},
					beforeSend:function(){
			            //art.dialog.tips("删除中...");
			        },
					success : function(data){
						if(data.success){
							refeshBrand();
						}else{
							artDialog.alert(data.msg , function(){refeshBrand();});
						}
					}
				});
			});
	}
</script>
